package com.ruoyi.shop.reservation.service.impl;

import java.awt.image.BufferedImage;
import java.util.List;
import java.util.UUID;

import com.ruoyi.system.api.RemoteFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.reservation.mapper.RepairReservationMapper;
import com.ruoyi.shop.reservation.domain.RepairReservation;
import com.ruoyi.shop.reservation.service.IRepairReservationService;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;



/**
 * 维修预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@Slf4j
@Service
public class RepairReservationServiceImpl implements IRepairReservationService 
{
    @Autowired
    private RepairReservationMapper repairReservationMapper;
    @Autowired
    private RemoteFileService remoteFileService;



   
    /**
     * 查询维修预约
     * 
     * @param id 维修预约主键
     * @return 维修预约
     */
    @Override
    public RepairReservation selectRepairReservationById(Long id)
    {
        return repairReservationMapper.selectRepairReservationById(id);
    }

    /**
     * 查询维修预约列表
     * 
     * @param repairReservation 维修预约
     * @return 维修预约
     */
    @Override
    public List<RepairReservation> selectRepairReservationList(RepairReservation repairReservation)
    {
        return repairReservationMapper.selectRepairReservationList(repairReservation);
    }

    /**
     * 新增维修预约
     *
     * @param repairReservation 维修预约
     * @return 结果
     */
    @Override
    public Long insertRepairReservation(RepairReservation repairReservation) {
        // 生成核销码
        String verificationCode = generateVerificationCode();
        repairReservation.setVerificationCode(verificationCode);

        MultipartFile qrCodeFile = null;
        String qrCodeUrl = null;

        try {
            // 生成二维码并获取 MultipartFile
            qrCodeFile = generateQRCode(verificationCode);
        } catch (WriterException | IOException e) {
            log.error("生成二维码失败", e);
            return -1L; // 返回失败代码或抛出自定义异常
        }

        try {
            // 上传二维码并获取URL
            qrCodeUrl = uploadQRCode(qrCodeFile);
        } catch (Exception e) {
            log.error("上传二维码失败", e);
            return -1L; // 返回失败代码或抛出自定义异常
        }

        // 设置二维码 URL
        repairReservation.setVerificationImg(qrCodeUrl);

        // 插入维修预约
        int result = repairReservationMapper.insertRepairReservation(repairReservation);
        if (result <= 0) {
            log.error("插入维修预约失败");
            return -1L; // 返回失败代码
        }

        return repairReservation.getId(); // 返回成功的结果
    }

    /**
     * 修改维修预约
     * 
     * @param repairReservation 维修预约
     * @return 结果
     */
    @Override
    public int updateRepairReservation(RepairReservation repairReservation)
    {
        return repairReservationMapper.updateRepairReservation(repairReservation);
    }

    /**
     * 批量删除维修预约
     * 
     * @param ids 需要删除的维修预约主键
     * @return 结果
     */
    @Override
    public int deleteRepairReservationByIds(Long[] ids)
    {
        return repairReservationMapper.deleteRepairReservationByIds(ids);
    }

    /**
     * 删除维修预约信息
     * 
     * @param id 维修预约主键
     * @return 结果
     */
    @Override
    public int deleteRepairReservationById(Long id)
    {
        return repairReservationMapper.deleteRepairReservationById(id);
    }

    // 假设有一个上传图片的方法
    private String uploadImage(MultipartFile image) {
        // 实现上传逻辑并返回URL
        return remoteFileService.upload(image).getData().getUrl(); // 示例URL
    }
     // 生成核销码
     private String generateVerificationCode() {
        return UUID.randomUUID().toString();
    }

    // 生成二维码
    private MultipartFile generateQRCode(String verificationCode) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(verificationCode, BarcodeFormat.QR_CODE, 200, 200);
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);

        byte[] fileContent = baos.toByteArray();
        String fileName = "qrcode.png";
        String mimeType = "image/png";

        return new MockMultipartFile(
                "file", // form field name
                fileName, // original filename
                mimeType, // MIME type
                fileContent // file content
        );
    }

    // 上传二维码并获取URL
    private String uploadQRCode(MultipartFile qrCodeImage) {
        // 实现上传逻辑，返回上传后的URL
        
        // 这里假设有一个uploadImage方法
        return uploadImage(qrCodeImage);
    }

}
